package com.ht.medicaltreatment.activity.haocai2

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ConsumableListAdapter
import com.ht.medicaltreatment.adapter.SubjectProgressAdapter
import com.ht.medicaltreatment.bean.ConsumableTypeProgress
import com.ht.medicaltreatment.viewmodel.ConsumableViewModel
import kotlinx.android.synthetic.main.activity_consumable_info_layout.imgAdd
import kotlinx.android.synthetic.main.activity_consumable_info_layout.listRecyclerView
import kotlinx.android.synthetic.main.activity_consumable_info_layout.recyclerView
import kotlinx.android.synthetic.main.activity_consumable_info_layout.titleName
import kotlinx.android.synthetic.main.activity_consumable_info_layout.titleProgress
import kotlinx.android.synthetic.main.activity_consumable_info_layout.tvApply
import kotlinx.android.synthetic.main.activity_consumable_info_layout.tvCount
import kotlinx.android.synthetic.main.activity_consumable_info_layout.tvProgress
import kotlinx.android.synthetic.main.activity_consumable_info_layout.tvSend

//耗材明细
class ConsumableListActivity : BaseActivity(), OnItemClickListener, View.OnClickListener {

    private val modle by viewModels<ConsumableViewModel>()
    private var id = 0
    private lateinit var progressAdapter:SubjectProgressAdapter
    private lateinit var adapter:ConsumableListAdapter

    override fun getLayoutId() = R.layout.activity_consumable_info_layout

    override fun init() {
        setTitle("耗材明细")

        recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        progressAdapter = SubjectProgressAdapter()
        recyclerView.adapter = progressAdapter
        //val progressAdapter = ProgressTubeAdapter()
        //recyclerView.adapter = progressAdapter
        //添加
        imgAdd.setOnClickListener {
            val intent = Intent(this,AddSubjectActivity::class.java)
            intent.putExtra("id",id)
            startActivity(intent)
        }

        listRecyclerView.layoutManager = LinearLayoutManager(this)
        adapter = ConsumableListAdapter()
        listRecyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)

        tvSend.setOnClickListener(this)
        tvApply.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        id = intent.getIntExtra("id",0)
        initData()
    }

    private fun initData() {
        showProgress()
        modle.consumableList(id,{
            dismissProgress()
            titleName.text = it.protocolNumber
            tvCount.text = "${it.projectConsumableNumber}"

            adapter.setNewInstance(it.consumableTypeProgressList)
            val split = it.totalConsumableUseProgress?.split("/")
            var progress = 0

            val pro = split[0].toFloat()
            val total = split[1].toFloat()
            if(total > 0){
                progress = (pro/total*100).toInt()
            }
            LogUtils.i(progress)
            tvProgress.setText("$progress %")
            titleProgress.text = "已使用：${it.totalConsumableUseProgress}"
            val progressList = mutableListOf(false,false,false,false,false,false,false,false,false,false)
            val p = progress / 10
            LogUtils.i(p)
            for (i in 1..p){
                var index = i-1
                if(index >= progressList.size)break
                progressList[index] = true
            }
            progressAdapter.setNewInstance(progressList)
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as ConsumableTypeProgress
        val intent = Intent(this,ConsumableDetailActivity::class.java)
        intent.putExtra("sign",item.sign)
        intent.putExtra("id",id)
        startActivity(intent)
    }

    override fun onClick(v: View?) {
        when(v){
            tvApply -> {
                val intent = Intent(this,ConsumableRecordActivity::class.java)
                intent.putExtra("id",id)
                startActivity(intent)
            }
            tvSend -> startActivity(SendListActivity::class.java)
        }
    }
}